const Service = require("egg").Service;

class HomeService extends Service {
  /**
   * 获取所有告警数量
   */
  async getDeviceAlarmCount() {
    const { app } = this;
    return await app.model.TDevState.findOne({
      attributes: [
        app.Sequelize.fn("sum", app.Sequelize.col("ss_WarnClass1Num")),
        app.Sequelize.fn("sum", app.Sequelize.col("ss_WarnClass2Num")),
        app.Sequelize.fn("sum", app.Sequelize.col("ss_WarnClass3Num")),
        app.Sequelize.fn("sum", app.Sequelize.col("ss_WarnClass4Num")),
      ],
    });
  }

  /**
   * 获取实时数据
   */
  async getTableData(table, fields = "*") {
    const { ctx, app } = this;

    if (!table) {
      ctx.throw(422, "数据表不存在");
    }

    const sql = ` select ${fields} from ${table} `;

    return await app.model.query(sql, {
      type: app.Sequelize.QueryTypes.SELECT,
      raw: true,
      replacements: {},
    });
  }
}

module.exports = HomeService;
